<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24" class="top">
        <el-form :model="searchParams" ref="searchParams" :inline="true">
          <el-form-item label="名称">
            <el-input class="input" v-model="searchParams.name" maxlength='20' placeholder="请输入" clearable
              show-word-limit></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="handleSearch" type="primary" icon="el-icon-search"
              style="margin-left:40px">搜索</el-button>
          </el-form-item>
        </el-form>
        <el-form :inline="true" style="margin:10px 0">
          <el-button @click="operateAction('create', {})" type="primary" plain size='small'>新增</el-button>
          <el-button @click="operateAction('delete', {})" type="info" plain size='small'>删除</el-button>
        </el-form>
        <el-table :data="dataList" size='medium' ref="multipleTable" @selection-change="handleSelectionChange" border>
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="id" label="id" width="80" show-overflow-tooltip></el-table-column>
          <el-table-column prop="coverUrl" label="封面" show-overflow-tooltip>
            <template slot-scope="{row, index}"><img width="50" height="50" :src="row.coverUrl"></template>
          </el-table-column>
          <el-table-column prop="name" label="名称" show-overflow-tooltip width="140"></el-table-column>
          <el-table-column prop="coursecateModel.name" label="课程分类" show-overflow-tooltip></el-table-column>
          <el-table-column prop="tags" label="标签" show-overflow-tooltip width="140">
            <template slot-scope="{row, index}">
              <div style="display: flex;flex-wrap: wrap;">
                <div v-for="(item, index) in row.tagList" style="border: 1px solid #ddd;padding: 2px 4px;margin: 2px;display: flex;justify-content: center;align-items: center;background: #FFF;">
                {{item.name}}
              </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="teacherModel.realname" label="教师" show-overflow-tooltip></el-table-column>
          <el-table-column prop="hour" label="课时" show-overflow-tooltip></el-table-column>
          <el-table-column prop="status" label="状态" show-overflow-tooltip>
              <template slot-scope="{row, index}">
                  <el-tag v-if="row.status==1" type="info">草稿</el-tag>
                  <el-tag v-if="row.status==2">已发布</el-tag>
              </template>
          </el-table-column>
          <el-table-column prop="startTime" label="开始时间" show-overflow-tooltip width="160">
            <template slot-scope="{row, index}">
              {{ formatTime.dateFormat('YY-MM-DD hh:mm:ss', row.startTime) }}
            </template>
          </el-table-column>
          <el-table-column prop="endTime" label="结束时间" show-overflow-tooltip width="160">
            <template slot-scope="{row, index}">
              {{ formatTime.dateFormat('YY-MM-DD hh:mm:ss', row.endTime) }}
            </template>
          </el-table-column>
          <el-table-column prop="lastTime" label="报名截止时间" show-overflow-tooltip width="160">
            <template slot-scope="{row, index}">
              {{ formatTime.dateFormat('YY-MM-DD hh:mm:ss', row.lastTime) }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间"show-overflow-tooltip  width="160">
            <template slot-scope="{row, index}">
              {{ formatTime.dateFormat('YY-MM-DD hh:mm:ss', row.createTime) }}
            </template>
          </el-table-column>
          <el-table-column prop="redact" label="操作" fixed="right" width="200">
            <template slot-scope="{ row, index }">
              <el-button type="text" @click="operateAction('update', row)" size='medium'>修改</el-button>
              <el-button type="text" @click="operateAction('detail', row)" size='medium'>详情</el-button>
              <el-button type="text" @click="operateAction('setCourse', row)" size='medium'>课程设计</el-button>
              <el-button type="text" @click="operateAction('setTeacher', row)" size='medium'>分配教师</el-button>
              <el-button type="text" @click="operateAction('checkSignUp', row)" size='medium'>查看报名</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination style="background: #ffffff; height: 50px" v-show="totalSize > 0" :total="totalSize"
          :page.sync="page" :limit.sync="size" @pagination="getDataList" />
      </el-col>
    </el-row>
    <setTeacherDialog ref="setTeacherDialog" @confirm="getDataList"></setTeacherDialog>
    <checkSignUpDialog ref="checkSignUpDialog" @confirm="getDataList"></checkSignUpDialog>
  </div>
</template>

<script>
import { formData } from './data'
import setTeacherDialog from './setTeacherDialog.vue'
import checkSignUpDialog from './checkSignUpDialog.vue'
export default {
  components: { setTeacherDialog, checkSignUpDialog },
  data() {
    return {
      page: 1,
      size: 10,
      totalSize: 0,
      dataList: [],
      searchParams: {},
      multipleSelection: [],
      formData
      
    }
  },
  created() {
    if (this.$route.query.currentSearchParams) {
      this.searchParams = JSON.parse((decodeURIComponent(this.$route.query.currentSearchParams)))
      this.page = this.searchParams.page
    }
    this.getDataList()
    this.findCoursecateList()
  },
  methods: {
    handleSearch() {
      this.page = 1
      this.getDataList()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    teacherConfirmBack(val){
      console.log('teacherConfirmBack', val)
    },
    operateAction(scene, row) {
      switch (scene) {
        case 'create':
          this.$router.push({name: 'Update', query: {formData: encodeURIComponent(JSON.stringify(this.formData)), currentRouteName: this.$route.name }})
          break;
        case 'update':
          this.$router.push({name: 'Update', query: {rowId: row.id, formData: encodeURIComponent(JSON.stringify(this.formData)), currentRouteName: this.$route.name, currentSearchParams: encodeURIComponent(JSON.stringify(this.searchParams)) }})
          break;
        case 'detail':
          this.$router.push({name: 'Detail', query: {rowId: row.id, formData: encodeURIComponent(JSON.stringify(this.formData)), currentRouteName: this.$route.name, currentSearchParams: encodeURIComponent(JSON.stringify(this.searchParams)) }})
          break;
        case 'setCourse':
          this.$router.push({name: 'setCourse', query: {row: row, formData: encodeURIComponent(JSON.stringify(this.formData)), currentRouteName: this.$route.name, currentSearchParams: encodeURIComponent(JSON.stringify(this.searchParams)) }})
          break;
        case 'setTeacher':
          this.$refs.setTeacherDialog.show(row)
          break;
        case 'checkSignUp':
          this.$refs.checkSignUpDialog.show(row)
          break;
        case 'delete':
          if (this.multipleSelection.length <= 0) {
            this.$message({
              message: '请选择操作行',
              type: 'info'
            })
          } else {
            var rowIds = this.multipleSelection.map(function (item) {
              return item.id
            })
            var _this = this
            this.$confirm('是否确认删除选中的' + rowIds.length + '条记录?', "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(function () {
              return _this.request_post(_this.formData.deleteUrl, {
                obj_ids: rowIds
              }).then(res => {
                if (res.data.code == 10000) {
                  _this.getDataList()
                  _this.$message({
                    message: '操作成功',
                    type: 'success'
                  })
                }
              })
            })
          }
          break;
        default:
          break;
      }
    },
    //数据请求-----------------------------------------------
    // 获取列表数据
    getDataList() {
      this.searchParams.page = this.page
      this.searchParams.size = this.size
      this.request_get(this.formData.getListUrl, this.searchParams).then(res => {
        if (res.data.code == 10000) {
          this.totalSize = res.data.data.total_count
          this.dataList = res.data.data.list
        }
      });
    },
    findCoursecateList() {
      this.request_get('/education/coursecate/').then(res => {
        if (res.data.code == 10000) {
          this.formData.options.coursecate = res.data.data.list
        }
      });
    },
  },
}
</script>


<style lang="scss" scoped></style>
